#!/usr/bin/env bash

set -e -o pipefail

PRERELEASE_PREFIX="prerelease/"
TAG_NAME="v${VERSION}"
EXISTING_REMOTE_TAG=$(git ls-remote origin "refs/tags/${TAG_NAME}")

if [[ "${CURRENT_BRANCH_NAME}" != "${PRERELEASE_PREFIX}"* ]]; then
  echo "script ran on '${CURRENT_BRANCH_NAME}, must be run on a branch that begins with '${PRERELEASE_PREFIX}'."
  exit 1
fi

if [ -n "${EXISTING_REMOTE_TAG}" ]; then
  echo "The tag ${TAG_NAME} already exists on github.com"
  echo "This likely means this version has already been published."
  echo "Please examine the tag ${TAG_NAME} on github.com/heroku/cli to see the contents of the ${TAG_NAME} release"
  exit 1
fi

# we are only allowing for alpha and beta channel releases currently.
if [[ "${CHANNEL}" != "alpha" ]] && [[ "${CHANNEL}" != "beta" ]] && [[ "${CHANNEL}" != "dev" ]]; then
  echo "script must be run on a branch with an alpha or a beta prerelease tag."
  echo "to create an alpha or beta prerelease tag, see the Heroku CLI release instructions on creating a prerelease"
  exit 1
fi
